package com.ant.storage.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Configurable;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hzd
 * @version 1.0
 * @date 2023/7/14 11:14
 */

@Slf4j
@Configurable
public class RequestUtil {

     private static final String URL = "http://182.140.221.205:9527";
    //private static final String URL = "https://os.17hc.com";
    /**
     * post 请求
     *
     * @param apiUrl
     * @param param
     * @return
     * @throws Exception
     */
    public static String post(String apiUrl, Map<String, Object> param, String token) throws Exception {
        try {
            String  str = HttpRequest.post(URL+apiUrl)
                    .header("Accept", "application/json")
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .header("Authorization", token==null?"":token)
                    .body(JSON.toJSONString(param)).timeout(20000).execute().body();
            return str;
        } catch (Exception e) {
            System.out.println("请求异常：" + e.getMessage());
            return e.getMessage();
        }
    }

    /**
     * @param c       需要转换的实体类
     * @param pathUrl 需要请求的接口路径
     * @param body    请求的参数
     */
    public static <T> T doPostJSON(Class<T> c, String pathUrl, String body) {
        HttpResponse execute = HttpRequest.post(URL+pathUrl)
                .headerMap(getJsonCommonHeader(), true) //请求的消息头
                .timeout(5)//请求超时时间
                .body(body)//请求的json格式的参数
                .execute();
        int status = execute.getStatus();
        if (200 == status || 201 == status) {
            String respBody = execute.body();//请求参数后的返回值
            T bean = JSONUtil.toBean(respBody, c);//将返回的数据转成自己的实体类
            return bean;
        } else {
            log.error("请求url:{}, 请求体：{}, 请求头：{}, 响应码异常：{}，响应体：{}");
        }
        return null;
    }

    /**
     * @param c       需要转换的实体类
     * @param pathUrl 需要请求的接口路径
     * @param formMap 请求的参数
     */
    public static <T> T doPostForm(Class<T> c, String pathUrl, Map<String, Object> formMap) {
        HttpResponse execute = HttpRequest.post(URL+pathUrl)
                .headerMap(getCommonHeader(), true)
                .timeout(3000)
                .form(formMap)
                .execute();
        int status = execute.getStatus();
        if (200 == status || 201 == status) {
            String respBody = execute.body();
            T bean = JSONUtil.toBean(respBody, c);
            return bean;
        } else {
            log.error("请求url:{}, 请求体：{}, 请求头：{}, 响应码异常：{}，响应体：{}", pathUrl, formMap, getCommonHeader(), status, execute);
        }
        return null;
    }

    /**
     * @param c 需要转换的实体类
     * @param pathUrl 需要请求的接口路径
     * @param headerMap 请求头的token
     * */
    public static <T> T doGetT(Class<T> c, String pathUrl, Map<String, String> headerMap) {
        HttpResponse execute = HttpRequest.get(URL+pathUrl)
                .headerMap(getCommonHeader(), true)
                .headerMap(headerMap, true)
                .timeout(3000)
                .execute();
        System.out.println(execute.body());
        int status = execute.getStatus();
        if (200 == status || 201 == status) {
            String respBody = execute.body();
            T bean = JSONUtil.toBean(respBody, c);
            return bean;
        } else {
            log.error("请求url:{}, 请求体：{}, 请求头：{}, 响应码异常：{}，响应体：{}");
        }
        return null;
    }

    //这个方法是表单格式的请求头的一些参数
    private static Map<String, String> getCommonHeader() {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("accept", "*/*");
        headerMap.put("connection", "Keep-Alive");  //维持长链接
        return headerMap;
    }
    //json格式的请求头
    private static Map<String, String> getJsonCommonHeader() {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("accept", "*/*");
        headerMap.put("connection", "Keep-Alive");  //维持长链接
        headerMap.put("Content-Type", "application/json;charset=utf-8");
        return headerMap;
    }

}
